<%--
  Created by IntelliJ IDEA.
  User: 86178
  Date: 2020/7/20
  Time: 16:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>登录页面</title>
    <!-- script标签 不能省后半部分 -->
    <script src="${pageContext.request.contextPath}/js/jquery-2.1.0.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/layui/layui.js"></script>


</head>
<body>
    <form class="layui-form" action="" >
        <div class="layui-form-item" style="margin-top: 10px">
            <label class="layui-form-label">邮箱:</label>
            <div class="layui-input-inline">
                <input type="text" name="email" required  lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码:</label>
            <div class="layui-input-inline">
                <input type="password" name="password" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-radius layui-btn-normal" lay-submit lay-filter="listenLogin">登录</button>
                <button type="reset" class="layui-btn layui-btn-radius" layui-btn-primary onclick="listenRegister()">注册</button>
            </div>
        </div>
    </form>

    <script>
        //Demo
        layui.use('form', function(){
            var form = layui.form;
            //监听登录
            form.on('submit(listenLogin)', function(datas){
                $.ajax({
                    url:"${pageContext.request.contextPath}/queryUser",
                    type:"get",
                    data: datas.field,
                    success:function (msg) {
                        if (msg.flag === "ok"){
                            console.log(msg)
                            window.alert("登录成功");
                            location.href="${pageContext.request.contextPath}/index.jsp";
                        }else if(msg.flag === null){
                            window.alert("邮箱或密码错误，请重新登录")
                            location.href="${pageContext.request.contextPath}/login.jsp";
                        }
                    }
                })
            });
        });

        function listenRegister() {
            location.href=" ${pageContext.request.contextPath}/regist.jsp";
        }
    </script>
</body>
</html>
